package com.bq.gametrade.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.CancelledRecordedDetails;
import com.bq.gametrade.utils.FormatUtil;

import java.util.ArrayList;

/**
 * 功能：
 * <p>
 *
 * @author Feel on 2016/12/13 15:53
 */

public class CancelledRecordedAdapter extends BaseAdapter {

    private ArrayList<CancelledRecordedDetails> items;
    private Context context;

    public CancelledRecordedAdapter(Context context, ArrayList<CancelledRecordedDetails> items) {
        this.context = context;
        this.items = items;
    }



    /**
     * ItemClick的回调接口
     *
     * @author feel
     */
    public interface OnItemClickLitener {
        void onItemClick(int position,String cancelledId);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


    public void setData(ArrayList<CancelledRecordedDetails> items) {
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_item_cancelledrecorded, null);
            viewHolder.tvCancelledTime = (TextView) convertView.findViewById(R.id.tv_cancelled_time);
            viewHolder.tvCancelledType = (TextView) convertView.findViewById(R.id.tv_cancelledType);
            viewHolder.tvCancelledMoney = (TextView) convertView.findViewById(R.id.tv_cancelledMoney);
            viewHolder.tvCancelledCount = (TextView) convertView.findViewById(R.id.tv_cancelledAccount);
            viewHolder.btnCancelled = (Button) convertView.findViewById(R.id.btn_cancel);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final CancelledRecordedDetails details = items.get(position);
        viewHolder.tvCancelledTime.setText(details.otime);
        viewHolder.tvCancelledCount.setText(details.ticket);
        viewHolder.tvCancelledMoney.setText(FormatUtil.valueFormat(details.money.replace(",",""))+"元");
        //0:买
       if(details.order_type.equals("0")){
            viewHolder.tvCancelledType.setTextColor(context.getResources().getColor(R.color.red_CE1419));
           viewHolder.tvCancelledType.setText("买");
       }else {
           viewHolder.tvCancelledType.setTextColor(context.getResources().getColor(R.color.green_28A71C));
           viewHolder.tvCancelledType.setText("卖");
       }

        viewHolder.btnCancelled.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(position,details.orderId);
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView tvCancelledTime;
        TextView tvCancelledType;
        TextView tvCancelledCount;
        TextView tvCancelledMoney;
        Button btnCancelled;
    }
}
